﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BuXiaChu.OrderDishes.Model;

namespace BuXiaChu.OrderDishes.BLL.Passport
{
    public class PassportManager
    {
        public static PassportManager Instance
        {
            get;
            private set;
        }

        static PassportManager()
        {
            Instance = new PassportManager();
            Instance.activeList.Clear();
        }
        private DateTime lastClearDate = DateTime.Now;
        private Dictionary<Guid, SysUserActiveModel> activeList = new Dictionary<Guid, SysUserActiveModel>();
        public Guid Create(int userId)
        {
            return Create(userId, -1);
        }

        public Guid Create(int userId, long ip)
        {
            #region
            SysUserActiveModel entity = new SysUserActiveModel();
            entity.SmartId = Guid.NewGuid();
            entity.UserId = userId;
            entity.Ip = ip;
            entity.CreateDate = DateTime.Now;
            entity.LastActiveDate = DateTime.Now;

            AddUserToActiveList(entity);

            return entity.SmartId;
            #endregion
        }

        public void Remove(Guid smartId)
        {
            activeList.Remove(smartId);
        }

        public bool Validate(Guid smartId, int userId)
        {
            var result = false;

            #region
            SysUserActiveModel entity = new SysUserActiveModel();
            if (activeList.TryGetValue(smartId, out entity))
            {
                if (entity.UserId == userId
                    && DateTime.Compare(entity.LastActiveDate.AddMinutes(40), DateTime.Now) > 0)
                {//40分钟过期
                    activeList[smartId].LastActiveDate = DateTime.Now;
                    result = true;
                }
                else
                {
                    Remove(smartId);
                }
            }
            #endregion

            #region 列表过期检测
            if (DateTime.Compare(lastClearDate, DateTime.Now.AddMinutes(-20)) < 0)
            {
                lastClearDate = DateTime.Now;
                Dictionary<Guid, SysUserActiveModel> tmpl = new Dictionary<Guid, SysUserActiveModel>();
                foreach (var item in activeList)
                {
                    if (DateTime.Compare(item.Value.LastActiveDate.AddMinutes(40), DateTime.Now) < 0)
                    {
                        tmpl.Add(item.Key, item.Value);
                    }
                }
                foreach (var item in tmpl)
                {
                    Remove(item.Key);
                }
            }
            #endregion

            return result;
        }

        private void AddUserToActiveList(SysUserActiveModel user)
        {
            #region
            Guid tmplGuid = Guid.Empty;
            foreach (var item in activeList)
            {
                if (item.Value.UserId == user.UserId)
                {
                    tmplGuid = item.Key;
                    break;
                }
            }
            if (tmplGuid != Guid.Empty)
            {
                Remove(tmplGuid);
            }

            activeList.Add(user.SmartId, user);
            #endregion
        }
    }
}
